from langchain.agents import initialize_agent, Tool
from langchain.llms import OpenAI
import requests

# Initialize the OpenAI LLM
llm = OpenAI(temperature=0)

def create_test_case(requirement):
    """
    Generate a test case based on the user's requirement.
    """
    prompt = f"Generate a test case for the following requirement: {requirement}"
    return llm(prompt)

def run_test_case(test_case):
    """
    Send the generated test case to the backend API for execution.
    """
    api_url = "http://localhost:5000/run_test_case"  # Replace with your backend API URL
    response = requests.post(api_url, json={"test_case": test_case})
    return response.json()

def validate_test_result(result):
    """
    Validate the result returned by the backend API.
    """
    if result.get("status") == "success":
        print("Test case executed successfully.")
    else:
        print("Test case execution failed.")

# Define the tools for the agent
tools = [
    Tool(
        name="Create Test Case",
        func=create_test_case,
        description="Generates a test case based on the user's requirement."
    ),
    Tool(
        name="Run Test Case",
        func=run_test_case,
        description="Sends the generated test case to the backend API for execution."
    ),
    Tool(
        name="Validate Test Result",
        func=validate_test_result,
        description="Validates the result returned by the backend API."
    )
]

# Initialize the agent
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)

if __name__ == "__main__":
    user_requirement = input("请输入测试需求: ")
    agent.run(user_requirement)
